package com.csap.framework.apidoc.admin.service.impl;

import com.csap.framework.annotation.apidoc.Description;
import com.csap.framework.apidoc.admin.entity.ApiDocEntity;
import com.csap.framework.apidoc.admin.model.ApiDocModel;
import com.csap.framework.apidoc.admin.model.freemark.ConfigModel;
import com.csap.framework.apidoc.admin.service.ApiDocService;
import com.csap.mybatisplus.extension.service.impl.BaseCsapServiceImpl;
import com.csap.framework.apidoc.admin.model.ApiDocPathModel;
import com.csap.framework.apidoc.admin.entity.ApiDocPathEntity;
import org.springframework.stereotype.Service;
import com.csap.framework.apidoc.admin.mybatis.ApiDocPathMapper;
import com.csap.framework.apidoc.admin.service.ApiDocPathService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文档路径信息
 * </p>
 *
 * @author
 * @dateTime
 */
@Service
@Description(value = "文档路径信息ServiceImpl")
public class ApiDocPathServiceImpl extends BaseCsapServiceImpl<ApiDocPathMapper, ApiDocPathEntity, ApiDocPathModel, Long> implements ApiDocPathService {
    @Resource
    private ApiDocService apiDocService;

    @Override
    public Boolean flushDoc(Long pathId) {
        List<ConfigModel> configModelList = lambdaQuery()
                .eq(ApiDocPathEntity::getId, pathId)
                .orderByDesc(ApiDocPathEntity::getSeq)
                .oneOptional()
                .transformOptional(ApiDocPathModel.class)
                .stream()
                .map(apiDocService::foreachDoc)
                .map(i -> apiDocService
                        .lambdaQuery()
                        .eq(ApiDocEntity::getId, i.getApidocId())
                        .oneOptional()
                        .transformOptional(ApiDocModel.class)
                        .map(i2 -> new ConfigModel(i2).setPathList(Arrays.asList(i))))
                .map(i3 -> i3.get())
                .collect(Collectors.toList());
        apiDocService.writeDocConfig(configModelList);
        return Boolean.TRUE;
    }
}
